import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.NoSuchElementException;
import java.util.Scanner;

/**
 * Created by LLAP on 2018/1/8.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Main extends Form implements ItemListener {
    private JComboBox<String> fromLine, toLine;
    private JComboBox<Station> fromStation, toStation;
    private JLabel resultCount;
    private JLabel resultDistance;
    private JLabel resultPrice;
    private Metro metro;
    private JPanel topPanel, centerPanel;

    private static String[] lines = {"一号线", "二号线", "三号线", "四号线", "十号线", "S1号线", "S8号线"};

    public Main() {
        this(new Metro());
    }
    public Main(Metro m) {
        super("", 400, 220);
        topPanel = new JPanel(new GridLayout(2, 2, 5, 5));
        centerPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        fromLine = new JComboBox<>();
        toLine = new JComboBox<>();
        fromStation = new JComboBox<>();
        toStation = new JComboBox<>();

        addLines(fromLine);
        addLines(toLine);

        resultCount = new JLabel("0", SwingConstants.LEFT);
        resultDistance = new JLabel("0", SwingConstants.LEFT);
        resultPrice = new JLabel("0", SwingConstants.LEFT);

        metro = m;

        fromLine.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                changeLine(fromStation, e.getItem().toString());
            }
        });
        toLine.addItemListener(e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                changeLine(toStation, e.getItem().toString());
            }
        });

        changeLine(fromStation, "一号线");
        changeLine(toStation, "一号线");

        fromStation.addItemListener(this);
        toStation.addItemListener(this);

//        findPath(metro.stations.get(2), metro.stations.get(15));
        topPanel.add(fromLine);
        topPanel.add(fromStation);
        topPanel.add(toLine);
        topPanel.add(toStation);
        centerPanel.add(new JLabel("经过车站数", SwingConstants.CENTER));
        centerPanel.add(resultCount);
        centerPanel.add(new JLabel("距离（公里）", SwingConstants.CENTER));
        centerPanel.add(resultDistance);
        centerPanel.add(new JLabel("票价（元）", SwingConstants.CENTER));
        centerPanel.add(resultPrice);
        this.add("Center", centerPanel);
        this.add("North", topPanel);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
            findPath((Station)fromStation.getSelectedItem(), (Station)toStation.getSelectedItem());
        }
    }

    private void addLines(JComboBox<String> source) {
        for (String i : lines) {
            source.addItem(i);
        }
    }

    private void changeLine(JComboBox<Station> source, String line) {
        source.removeAllItems();
        for (Station i : metro.stations) {
            if (i.getLine().equals(line)) source.addItem(i);
        }
        topPanel.updateUI();
    }

    private void findPath(Station from, Station to) {
        Result r = metro.find(from, to);
        System.out.println(r.toString());
        resultPrice.setText(String.valueOf(r.price));
        resultCount.setText(String.valueOf(r.count));
        resultDistance.setText(String.format("%.3f", r.dis));
        centerPanel.updateUI();
    }

    public static void main(String[] args) {
        Metro m = new Metro();
        try {
            Scanner in = new Scanner(new File("PathFinder\\Stations.txt"));
            String instr;
            while ((instr = in.nextLine()) != null) {
//                System.out.println(instr);
                if (instr.equals("")) continue;
                String[] info = instr.split("\\s");
                m.add(new Station(info[0], info[1]));
            }
        }
        catch (NoSuchElementException e) {

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Scanner in = new Scanner(new File("PathFinder\\Path.txt"));
            while(true) {
                int from = in.nextInt();
                int to = in.nextInt();
                double dis = in.nextDouble();
                m.addPath(from, to, dis);
            }
        }
        catch (NoSuchElementException e) {

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Scanner in = new Scanner(new File("PathFinder\\InterChange.txt"));
            while(true) {
                int from = in.nextInt();
                int to = in.nextInt();
                double dis = in.nextDouble();
                m.addPath(from, to, dis);
            }
        }
        catch (NoSuchElementException e) {

        }
        catch (Exception e) {
            e.printStackTrace();
        }

        new Main(m).setVisible(true);
    }
}
